package com.example.demo.controller;

import com.example.demo.bean.ApiResponse;
import com.example.demo.entity.Book;
import com.example.demo.service.BookService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/books")
public class BookController {

    private static final Logger logger = LoggerFactory.getLogger(BookController.class);

    @Autowired
    private BookService bookService;

    @SuppressWarnings("rawtypes")
    @PostMapping("/upload")
    public ResponseEntity<ApiResponse> uploadExcel(@RequestParam("file") MultipartFile file) {
        try {
            logger.info("正在上传中------------------------------------");
            bookService.importBooksFromExcel(file);
            ApiResponse response = new ApiResponse<>(true, "上传成功", null);
            return ResponseEntity.ok(response);
        } catch (IOException e) {
            ApiResponse response = new ApiResponse<>(false, e.getMessage(), null);
            return ResponseEntity.badRequest().body(response);
        }
    }

    @GetMapping("/list")
    public ResponseEntity<ApiResponse<Page<Book>>> getAllBooks(
            @RequestParam(value = "page", defaultValue = "0") Integer page,
            @RequestParam(value = "size", defaultValue = "10") Integer size) {
        Page<Book> books = bookService.getBooks(page, size);

        return ResponseEntity.ok(new ApiResponse<>(true, "查询成功", books));
    }

    @GetMapping("/tail")
    public ResponseEntity<ApiResponse<List<Book>>> getTailBooks() {
        List<Book> result = bookService.getTailBooks();
        return ResponseEntity.ok(new ApiResponse<>(true, "查询成功", result));
    }

    @PostMapping("/add")
    public ResponseEntity<ApiResponse<Book>> addBook(@RequestBody Book book) {
        Book savedBook = bookService.saveBook(book);
        return ResponseEntity.ok(new ApiResponse<>(true, "书本保存成功", savedBook));
    }

    @PostMapping("/update/fileurl")
    public ResponseEntity<Book> updateBookFileUrl(@RequestBody Book book) {
        try {
            Book updatedBook = bookService.updateBookFileUrl(book.getBookId(), book.getFileUrl());
            return ResponseEntity.ok(updatedBook);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.notFound().build();
        }
    }

    @PostMapping("/update/booktype")
    public ResponseEntity<Book> updateBookType(@RequestBody Book book) {
        try {
            Book updatedBook = bookService.updateBookType(book.getBookId(), book.getTypeId());
            return ResponseEntity.ok(updatedBook);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.notFound().build();
        }
    }

    @PostMapping("/update/status")
    public ResponseEntity<ApiResponse<Book>> updateBookStatus(@RequestBody Book book) {
        try {
            Book updatedBook = bookService.updateBookStatus(book.getBookId(), book.getStatus());
            return ResponseEntity.ok(new ApiResponse<>(true, "查询成功", updatedBook));
        } catch (IllegalArgumentException e) {
            return ResponseEntity.notFound().build();
        }
    }

    // 添加搜索接口
    @GetMapping("/search")
    public ResponseEntity<ApiResponse<Page<Book>>> searchBooks(@RequestParam String name, @RequestParam int page,
            @RequestParam int size) {
        // return bookService.searchBooksByName(name);

        try {
            Page<Book> bookList = bookService.searchBooksByName(name, page, size);
            return ResponseEntity.ok(new ApiResponse<>(true, "查询成功", bookList));
        } catch (IllegalArgumentException e) {
            return ResponseEntity.notFound().build();
        }
    }

    @GetMapping("/{id}")
    public ResponseEntity<ApiResponse<Book>> getBookById(@PathVariable int id) {
        try {
            Book book = bookService.getBookById(id);
            return ResponseEntity.ok(new ApiResponse<>(true, "查询成功", book));
        } catch (IllegalArgumentException e) {
            return ResponseEntity.notFound().build();
        }
    }
}